#include "sessionfriendarea.h"

#include <QWidget>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QPushButton>
#include <QStyleOption>
#include <QPainter>
#include "debug.h"
#include "model/data.h"

// Qt 中继承自 QWidget 的类，通常会在构造函数中接收一个 parent 参数
// 这个参数用于指定当前控件的父控件，从而建立父子关系

SessionFriendArea::SessionFriendArea(QWidget *parent)
    : QScrollArea{parent}
{
    // 1. 设置滚动条的属性及相关样式
    this->setWidgetResizable(true);
    QString bar_style = "QScrollBar:vertical { width: 3px; }";
    bar_style += "QScrollBar:vertical { background-color: rgb(195, 192, 190); }";
    this->verticalScrollBar()->setStyleSheet(bar_style);
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");

    this->setStyleSheet("QScrollBar { border: none; }");

    // 2. 创建 QWidget
    container = new QWidget();
    container->setFixedWidth(250);
    container->setStyleSheet("QWidget { background-color: rgb(230, 230, 230); }");
    this->setWidget(container);

    // 3. 设置纵置布局
    QVBoxLayout* layout = new QVBoxLayout();
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->setAlignment(Qt::AlignTop);

    container->setLayout(layout);

#if UI_TEST
    // debug

    QIcon default_avater(":/resource/image/myAvatar.jpg");
    for (int i = 0; i < 30; i++)
    {
        QString name = "月朗天门-" + QString::number(i);
        QString text = "测试信息: " + QString::number(i);
        this->AddItem(SESSION_ITEM, "", default_avater, name, text);
    }
#endif
}

void SessionFriendArea::AddItem(ItemType type, const QString& id, const QIcon& avatar, const QString& name, const QString& text)
{
    SessionFriendItem* item = nullptr;
    if (type == SESSION_ITEM)
    {
        item = new SessionItem(this, id, avatar, name, text);
    }
    else if (type == FRIEND_ITEM)
    {
        item = new FriendItem(this, id, avatar, name, text);
    }
    else
    {
        LOG() << "未知 Item 类型";
        return;
    }
    // this->layout()->addWidget(item); // error
    container->layout()->addWidget(item);
}


SessionFriendItem::SessionFriendItem(QWidget* owner, const QIcon& avatar, const QString& name, const QString& text)
    : owner(owner)
{
    // 1. 设置项目的样式
    this->setFixedHeight(64);
    this->setStyleSheet("QWidget { background-color: rgb(236, 233, 231); }");

    // 2. 设置网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(8, 0, 0, 0);
    layout->setVerticalSpacing(0);
    layout->setHorizontalSpacing(0);

    this->setLayout(layout);

    // 3. 创建头像
    QPushButton* avatar_btn = new QPushButton();
    avatar_btn->setFixedSize(QSize(46, 46)); // 设置控件大小
    avatar_btn->setIconSize(QSize(46, 46));    // 设置图标大小
    avatar_btn->setIcon(avatar);
    avatar_btn->setStyleSheet("QPushButton { border: none; }");

    // avatar_btn->setFixedHeight(50);
    // avatar_btn->setFixedWidth(50);

    // 4. 添加名字
    QLabel* name_label = new QLabel();
    name_label->setText(name);
    name_label->setStyleSheet("QLabel { font-size: 14px; }");
    name_label->setFixedHeight(30);

    // 5. 添加预览消息(最新消息，或个性签名)
    message_label = new QLabel();
    message_label->setText(text);
    message_label->setFixedHeight(25);
    message_label->setStyleSheet("QLabel { font-size: 12px; }");

    // 6. 设置布局
    layout->addWidget(avatar_btn, 0, 0, 2, 2); // 头像放置于第0行 第0列，占据 2行 2列
    layout->addWidget(name_label, 0, 2, 1, 6);
    layout->addWidget(message_label, 1, 2, 1, 6);
}

void SessionFriendItem::paintEvent(QPaintEvent* event)
{
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

void SessionFriendItem::select()
{
    // 1. 恢复其它兄弟 Item 的 rgb
    QObjectList children = this->parent()->children(); // 获取父级窗口的所有子部件
    for (QObject* object : children)
    {
        if (object->isWidgetType() == false)
            continue;

        SessionFriendItem* temp = dynamic_cast<SessionFriendItem*>(object);
        if (temp->selected)
        {
            temp->selected = false;
            // temp->setStyleSheet("QWidget { background-color: rgb(230, 230, 230); }");
            temp->setStyleSheet("QWidget { background-color: rgb(236, 233, 231); }");
        }
    }

    // 2. 设置当前 Item 的 rgb
    selected = true;
    this->setStyleSheet("QWidget { background-color: rgb(200, 199, 198); }");
}

// 鼠标点击事件
void SessionFriendItem::mousePressEvent(QMouseEvent* event)
{
    select();
}

// 鼠标悬置事件
void SessionFriendItem::enterEvent(QEnterEvent* event)
{
    if (selected) return;
    this->setStyleSheet("QWidget { background-color: rgb(222, 220, 218); }");
}

// 鼠标移开事件
void SessionFriendItem::leaveEvent(QEvent* event)
{
    if (selected) return;
    // this->setStyleSheet("QWidget { background-color: rgb(230, 230, 230); }");
    // 一定要确保，鼠标移开时设置的 rgb 和初始 rgb 相同
    this->setStyleSheet("QWidget { background-color: rgb(236, 233, 231); }");
}

SessionItem::SessionItem(QWidget* owner, const QString& id, const QIcon& avatar, const QString& name, const QString& text)
    : SessionFriendItem(owner, avatar, name, text)
    , session_id(id)
    , text(text)
{

}

FriendItem::FriendItem(QWidget* owner, const QString& id, const QIcon& avatar, const QString& name, const QString& description)
    : SessionFriendItem(owner, avatar, name, description)
    , user_id(id)
{

}
